package Card.connect;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import Card.game.CountPlayer;

import com.lloseng.ocsf.client.ObservableClient;

/*
 * Represent as the client who play the game.
 * @author Juthamas Utamaphethai 5510546964
 * @author Natcha Chidbhob 5510546026
 */
public class CardClient extends ObservableClient implements Serializable {
	/** The ID of this player. */
	private int id;
	/** Check whether if this player go to start. */
	private boolean checkStart;
	/** The initial money of player. */
	private int initMoney;
	/** Bet money that this player give in each round. */
	private int bet;

	/*
	 * Initialize value of bet, initial money and set the port and host.
	 * 
	 * @param host
	 * 
	 * @param port
	 */
	public CardClient(String host, int port) {
		super(host, port);
		this.bet = 10;
		this.initMoney = 100;
	}

	@Override
	protected void handleMessageFromServer(Object msg) {
		List<String> keep = new ArrayList<String>();

		if (msg instanceof Integer) {
			int m = (int) msg;
			System.out.println("client money : " + m);

			if (m == this.getMoney()) {
				System.out.println("Card Client send id to server");
				String b = this.getID() + "";
				System.out.println("My ID : " + b);
				String[] myID = { "id", b };
				for (String v : myID) {
					System.out.println(v);
				}
				try {
					this.sendToServer(myID);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
		if (msg instanceof CountPlayer) {
			CountPlayer count = (CountPlayer) msg;
			id = count.getId();
		} else if (msg instanceof Boolean) {
			Boolean bo = (Boolean) msg;
			checkStart = bo;
		}
		if (msg instanceof int[]) {
			int[] allBet = (int[]) msg;
			for (int k : allBet) {
				System.out.println("All bet : " + k);
			}
			if (allBet[0] == id) {
				System.out.println("check get id");
				System.out.println(initMoney);
				System.out.println("bet :" + allBet[1]);
				initMoney += allBet[1];
				System.out.println(initMoney);
			}
			setChanged();
			notifyObservers(allBet);
		} else {
			setChanged();
			notifyObservers(msg);
		}

		if (msg instanceof String[]) {
			String[] member = (String[]) msg;
			if (member[0].equals("bet")) {
				bet = Integer.parseInt(member[1]);
				try {
					setInitMoney(initMoney, bet);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (member[0].equals("card")) {
				for (String v : member) {
					keep.add(v);
				}
				System.out.println(id);
				for (int i = 0; i < id - 1; i++) {
					keep.remove(member[i + 1]);
					keep.add(member[i + 1]);
				}
				System.out.println(keep);
				setChanged();
				notifyObservers(keep.toArray(new String[keep.size()]));
			}

		}

	}

	/*
	 * Tell the ID of this player.
	 * 
	 * @return id.
	 */
	public int getID() {
		return id;
	}

	/*
	 * Tell whether if this client is start.
	 * 
	 * @return true if this client has already started.
	 */
	public boolean isCheckStart() {
		return checkStart;
	}

	/*
	 * Give the balance of client to server.
	 * 
	 * @return a the balance.
	 */
	public String[] getInitMoney() throws IOException {
		setInitMoney(getMoney(), getBet());
		String init = initMoney + "";
		String[] a = { "init", init };
		for (String v : a) {
			System.out.println(v);
		}
		return a;
	}

	/*
	 * Tell the balance of client.
	 * 
	 * @return initMoney the balance.
	 */
	public int getMoney() {
		return initMoney;
	}

	/*
	 * Tell the bet money.
	 * 
	 * @return bet
	 */
	public int getBet() {
		return bet;
	}

	/*
	 * Set the balance after client give bet to game.
	 * 
	 * @param initMoney the balance.
	 * 
	 * @param bet
	 */
	public void setInitMoney(int initMoney, int bet) throws IOException {
		this.initMoney = initMoney - bet;
	}

	/*
	 * Give the bet of client to server.
	 * 
	 * @return a the bet.
	 */
	public String[] getBetMoney() {
		String b = bet + "";
		String[] a = { "bet", b };
		for (String v : a) {
			System.out.println(v);
		}
		return a;
	}

	// when the new bet money
	/*
	 * Set the bet if client change the value of bet money.
	 * 
	 * @param bet.
	 */
	public void setBet(int bet) {
		this.bet = bet;
	}

}
